package com.example.hhhh;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

/**
 * Created by Mr.Egg on 2016/11/19.
 */
public class DiscountView extends android.support.v7.widget.AppCompatTextView {

    private int defaultRotate = 315;
    private String labelText = "default";
    private int labelColor = Color.WHITE;
    private int textColor = Color.BLACK;

    public DiscountView(Context context) {
        super(context);
    }

    public DiscountView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DiscountView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();


        //draw a translucent in top
        Paint timPaint = new Paint();
        timPaint.setColor(Color.parseColor("#ffffff"));
        timPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0f, 0f, width, height / 2f, timPaint);

        //draw a left mark
        Paint discountPaint = new Paint();
        discountPaint.setColor(labelColor);
        discountPaint.setStyle(Paint.Style.FILL);
        canvas.rotate(defaultRotate, 0, height / 2 + height / 8);
        canvas.translate(0, height / 8);
        Rect rect = new Rect(0, height / 4, width, height / 2);
        canvas.drawRect(rect, discountPaint);

        //draw the left mark text
        Paint textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.LEFT);
        textPaint.setTextSize(rect.height() / 2);
        canvas.drawText(labelText, rect.left + height / 7, height / 2 - rect.bottom / 7, textPaint);

    }

    public DiscountView retateView(int rotate) {
        this.defaultRotate = rotate;
        return this;
    }

    public DiscountView setLabelText(String str) {
        this.labelText = str;
        return this;
    }

    public DiscountView setLabelColor(int labelColor) {
        this.labelColor = labelColor;
        return this;
    }
    public DiscountView setLabelTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }
}
